Skip to content

Conversation

@Sriharika1506
Copy link

Issue : #112631

This patch adds support for constant folding for certain math library functions wherever it was previously missing.

@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Nov 28, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Raja Sudha Sri Harika (Sriharika1506)

Changes

Issue : #112631

This patch adds support for constant folding for certain math library functions wherever it was previously missing.


Full diff: https://github.com/llvm/llvm-project/pull/169893.diff

2 Files Affected:

  • (modified) llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h (+3)
  • (modified) llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp (+29)
diff --git a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
index 4e7c97194cc59..72b8f4b427b3b 100644
--- a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
+++ b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
@@ -214,6 +214,9 @@ class LibCallSimplifier {
   Value *optimizeSymmetric(CallInst *CI, LibFunc Func, IRBuilderBase &B);
   Value *optimizeRemquo(CallInst *CI, IRBuilderBase &B);
   Value *optimizeFdim(CallInst *CI, IRBuilderBase &B);
+
+  Value *foldLdexp(CallInst *CI, IRBuilderBase &B);
+  
   // Wrapper for all floating point library call optimizations
   Value *optimizeFloatingPointLibCall(CallInst *CI, LibFunc Func,
                                       IRBuilderBase &B);
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 4a1565977b91c..7508048d3032e 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -2464,6 +2464,24 @@ Value *LibCallSimplifier::optimizePow(CallInst *Pow, IRBuilderBase &B) {
   return nullptr;
 }
 
+Value *LibCallSimplifier::foldLdexp(CallInst *CI,IRBuilderBase &B) {
+
+  Type*RequiredType = CI->getType();
+  if(!RequiredType->isFPOrFPVectorTy())
+    return nullptr;
+
+  Value*x = CI->getArgOperand(0); 
+  Value*exp = CI->getArgOperand(1);
+  Module*M = CI->getModule();
+
+  Function*ldexpDecl = llvm::Intrinsic::getOrInsertDeclaration(M, Intrinsic::ldexp,{RequiredType, exp->getType()});
+  
+  CallInst *NewCall = B.CreateCall(ldexpDecl, {x, exp}, CI->getName());
+  NewCall->setAttributes(CI->getAttributes());
+  return copyFlags(*CI, NewCall);
+}
+
+
 Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilderBase &B) {
   Module *M = CI->getModule();
   Function *Callee = CI->getCalledFunction();
@@ -4031,6 +4049,17 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
   case LibFunc_exp2:
   case LibFunc_exp2f:
     return optimizeExp2(CI, Builder);
+  // New ldexp / scalbn / scalbln family:
+  case LibFunc_ldexp:
+  case LibFunc_ldexpf:
+  case LibFunc_ldexpl:
+  case LibFunc_scalbn:
+  case LibFunc_scalbnf:
+  case LibFunc_scalbnl:
+  case LibFunc_scalbln:
+  case LibFunc_scalblnf:
+  case LibFunc_scalblnl:
+    return foldLdexp(CI, Builder);
   case LibFunc_fabsf:
   case LibFunc_fabs:
   case LibFunc_fabsl:

@Sriharika1506 Sriharika1506 marked this pull request as draft November 28, 2025 10:19
@Sriharika1506 Sriharika1506 marked this pull request as ready for review November 28, 2025 10:23
@Sriharika1506
Copy link
Author

@ashwinagrl @YogeshFebyani also worked on this

@Sriharika1506
Copy link
Author

@dtcxzyw

@fawdlstty
Copy link
Contributor

Consider referencing PR #114417 and adding corresponding tests

@dtcxzyw dtcxzyw requested a review from arsenm November 28, 2025 13:18
@github-actions
Copy link

github-actions bot commented Nov 28, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Member

@dtcxzyw dtcxzyw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest modifying the code based on https://github.com/llvm/llvm-project/pull/114417/files and fixing previous comments.

I am a bit confused that you are fixing an issue that was assigned to @ashwinagrl. Have you two communicated with each other?

@Sriharika1506
Copy link
Author

@dtcxzyw We are working in a team along with @ashwinagrl.

@github-actions
Copy link

github-actions bot commented Nov 28, 2025

🐧 Linux x64 Test Results

The build failed before running any tests. Click on a failure below to see the details.

lib/Transforms/Utils/CMakeFiles/LLVMTransformUtils.dir/SimplifyLibCalls.cpp.o
FAILED: lib/Transforms/Utils/CMakeFiles/LLVMTransformUtils.dir/SimplifyLibCalls.cpp.o
sccache /opt/llvm/bin/clang++ -DGTEST_HAS_RTTI=0 -D_DEBUG -D_GLIBCXX_ASSERTIONS -D_GLIBCXX_USE_CXX11_ABI=1 -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/gha/actions-runner/_work/llvm-project/llvm-project/build/lib/Transforms/Utils -I/home/gha/actions-runner/_work/llvm-project/llvm-project/llvm/lib/Transforms/Utils -I/home/gha/actions-runner/_work/llvm-project/llvm-project/build/include -I/home/gha/actions-runner/_work/llvm-project/llvm-project/llvm/include -gmlt -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wc++98-compat-extra-semi -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wno-pass-failed -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -O3 -DNDEBUG -std=c++17  -fno-exceptions -funwind-tables -fno-rtti -UNDEBUG -MD -MT lib/Transforms/Utils/CMakeFiles/LLVMTransformUtils.dir/SimplifyLibCalls.cpp.o -MF lib/Transforms/Utils/CMakeFiles/LLVMTransformUtils.dir/SimplifyLibCalls.cpp.o.d -o lib/Transforms/Utils/CMakeFiles/LLVMTransformUtils.dir/SimplifyLibCalls.cpp.o -c /home/gha/actions-runner/_work/llvm-project/llvm-project/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
/home/gha/actions-runner/_work/llvm-project/llvm-project/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp:4053:8: error: use of undeclared identifier 'LibFunc_'
4053 |   case LibFunc_:
|        ^~~~~~~~
1 error generated.

If these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the infrastructure label.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants